package com.adwin.addcoin.ui.seetting

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import androidx.core.view.marginTop
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.lifecycleScope
import com.adwin.addcoin.AddCoinApplication
import com.adwin.addcoin.R
import com.adwin.addcoin.base.BaseActivity
import com.adwin.addcoin.databinding.ActivitySettingBinding
import com.adwin.addcoin.ui.dialog.ClearCacheDialog
import com.adwin.addcoin.ui.dialog.PrivacyPolicyDialog
import com.adwin.addcoin.ui.login.LoginActivity
import com.adwin.addcoin.utils.CacheDataManager
import com.adwin.addcoin.utils.NotificationUtil
import com.adwin.addcoin.utils.dataStore
import com.adwin.addcoin.utils.installRepost
import com.adwin.addcoin.utils.px2dp
import com.adwin.addcoin.utils.saveToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

class SettingActivity :BaseActivity<ActivitySettingBinding,SettingModel>(SettingModel::class.java){

    private var cacheDialog:ClearCacheDialog? = null

    private var isNotify = false

    override fun inflater(): ActivitySettingBinding {
        val binding = ActivitySettingBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)
        return binding
    }

    override fun initActivity() {
        fullScreen(this)
        setOnClickListener()
        cacheDialog = ClearCacheDialog(this)
        cacheDialog?.setOnDismissListener {
            updateCache()
        }
        viewModel?.isLogin?.observe(this){
            if (!it){
                lifecycleScope.launch (Dispatchers.IO){
                    dataStore.saveToken("")
                }
                val intent = Intent(this@SettingActivity,LoginActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(intent)
            }
        }
    }

    /**
     * 查询缓存数据
     */
    private fun updateCache(){
        lifecycleScope.launch(Dispatchers.IO){
            val cacheSize = CacheDataManager.getTotalCacheSize(this@SettingActivity)
            withContext(Dispatchers.Main){
                binding.tvCacheNumber.text = "${cacheSize?:"0M"}"
            }
        }
    }

    override fun onStart() {
        super.onStart()
        isNotify = NotificationUtil.isNotifyEnabled(this)       //查询通知权限是否开启
        if (isNotify){
            binding.ivOpenNotifications.setImageResource(R.mipmap.open_notifications_icon)
        }else{
            binding.ivOpenNotifications.setImageResource(R.mipmap.shut_down_notifications_icon)
        }
//        if (AddCoinApplication.instance.isGuestLogin){
//            binding.tvDeregister.visibility = View.GONE
//        }
        updateCache()
    }

    private fun setOnClickListener(){
        binding.ivOpenNotifications.setOnClickListener {
            MainScope().launch (Dispatchers.IO){
                val jsonObject = JSONObject()
                jsonObject.put("isNotify", isNotify.toString())
                installRepost("OpenNotification",jsonObject)
            }
            NotificationUtil.gotoSet(this)
        }
        binding.tvLogOut.setOnClickListener {
            viewModel?.logOut(this)
            MainScope().launch (Dispatchers.IO){
                installRepost("loginOutClick")
            }
        }
        binding.ivReturn.setOnClickListener {
            MainScope().launch (Dispatchers.IO){
                installRepost("SettingReturn")
            }
            finish()
        }
        binding.tvUserPrivacyPolicy.setOnClickListener {
            MainScope().launch (Dispatchers.IO){
                installRepost("settingPrivacyPolicyClick")
            }
            PrivacyPolicyDialog(this).show()
        }
        binding.tvClearCache.setOnClickListener {
            cacheDialog?.show()
        }
        binding.tvDeregister.setOnClickListener {
            viewModel?.userDelete(this)
        }
    }
}